//
//  VideoListVC.swift
//  CollectionViewDemo
//
//  Created by Met on 2022/6/1.
//

import UIKit

class VideoListVC1: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}



enum ListType: Int{
    case wechat = 0
    case bilibili = 1
    case tiktok = 2
}

class VideoListVC: UIViewController, UICollectionViewDataSource {
    var collectView: UICollectionView?
    var datas: [CellModel]?
    var type: ListType = .wechat
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = .white
        
        self.setupCollectView()
        
        self.loadDatas()
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        
        self.collectView?.frame = self.view.bounds
    }
    
    // MARK: - LoadDatas
    func loadDatas() {
        
        let tis = ["微信视频cell","B站视频Cell", "抖音视频cell"]
        var arr = [CellModel]()
        for _ in 0..<10 {
            let m = CellModel()
            m.title = tis[self.type.rawValue]
            m.cover = "flower.jpeg"
            arr.append(m)
        }
        self.datas = arr
        self.collectView?.reloadData()
    }
    
    // MARK: - SetupCollectView
    func setupCollectView() {
        var itemH: CGFloat = 0.0
        if type == .wechat {
            itemH = 188.0
        }else if type == .bilibili {
            itemH = 177.0
        }else if type == .tiktok {
            itemH = 285.0
        }
        itemH += 10.0
        
        let gap: CGFloat = 15.0
        let layout = UICollectionViewFlowLayout()
        
        layout.minimumLineSpacing = gap
        layout.minimumInteritemSpacing = gap
        layout.sectionInset = .init(top: 15, left: gap, bottom: 15, right: gap)
        let screenWidth = UIScreen.main.bounds.width
        layout.itemSize = .init(width: (screenWidth-3.0*gap)/2.0, height: itemH)
        
        let cv = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        cv.backgroundColor = .white
        cv.dataSource = self
        self.view.addSubview(cv)
        
        ///这里分别注册不同类型的cell
        if type == .wechat {
            cv.register(WechatVideoCell.self, forCellWithReuseIdentifier: "cell")
        }else if type == .bilibili{
            cv.register(BiliVideoCell.self, forCellWithReuseIdentifier: "cell")
        }else if type == .tiktok {
            cv.register(TiktokVideoCell.self, forCellWithReuseIdentifier: "cell")
        }
        
        self.collectView = cv
    }
    
    // MARK: - CollectViewDelegate
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.datas?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? VideoCell
        cell?.model = self.datas?[indexPath.row]
        
        return cell ?? UICollectionViewCell()
    }
}

// MARK: - Cell
class VideoCell: UICollectionViewCell {
    
    var titleL = UILabel()
    var coverView = UIImageView()
    var timeL = UILabel()
    
    var model: CellModel?{
        didSet{
            titleL.text = model?.title
            coverView.image = UIImage.init(named: model?.cover ?? "")
        }
    }
    
    init(frame: CGRect, type: ListType) {
        super.init(frame: frame)
            
        //针对不同的类型，做你想做的事 😄
        var titleH: CGFloat = 20.0
        if type == .wechat {
            titleH = 20.0
            VStack(titleL,coverView).embedIn(self.contentView)
        }else if type == .bilibili {
            titleH = 30.0
            
            VStack(coverView).embedIn(self.contentView)
            HStack(titleL).embedIn(self.contentView).align(.center)
            
        }else if type == .tiktok {
            titleH = 40.0
            
            VStack(coverView,titleL).embedIn(self.contentView)
        }
        
        titleL.align(.center).color("#FFF")
        titleL.pin(.h(titleH),.lowHugging).bg("random")
        coverView.mode(.scaleAspectFill).pin(.lowHugging)
        
        
        self.backgroundColor = Color("#EEE")
        
        coverView.clipsToBounds = true
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class CellModel: NSObject {
    var title: String?
    var cover: String?
}


// MARK: - 用来作为传参的子cell
class WechatVideoCell: VideoCell{
    override init(frame: CGRect) {
        //这里调用父类的时候，指定类型
        super.init(frame: frame, type: .wechat)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class BiliVideoCell: VideoCell{
    override init(frame: CGRect) {
        //这里调用父类的时候，指定类型
        super.init(frame: frame, type: .bilibili)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class TiktokVideoCell: VideoCell{
    override init(frame: CGRect) {
        //这里调用父类的时候，指定类型
        super.init(frame: frame, type: .tiktok)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

